package com.atguigu.educms.controller;


import com.atguigu.common.R;
import com.atguigu.common.cron.bean.PageListBean;
import com.atguigu.educms.entity.CmsLinks;
import com.atguigu.educms.service.CmsLinksService;
import com.atguigu.servicebase.exceptionhandler.ELeanExeception;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 友情链接 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/educms/cms-links")
@Api(tags = {"友情链接"})
public class CmsLinksController {

    @Autowired
    private CmsLinksService linksService;

    @ApiOperation(value = "获取友情链接信息")
    @PostMapping("/pageLinksConditions/{pageNo}/{pageSize}")
    public R pageLinksConditions(@PathVariable("pageNo") Integer pageNo,
                                 @PathVariable("pageSize") Integer pageSize,
                                 @RequestBody(required = false) CmsLinks cmsLinks) {
        PageListBean pageListBean = linksService.pageLinksConditions(pageNo, pageSize, cmsLinks);
        return R.ok().data(pageListBean);
    }

    @ApiOperation(value = "获取友情链接信息")
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") Integer id) {
        try {
            return R.ok().data("link", linksService.getById(id));
        } catch (Exception e) {
            return R.error();
        }
    }

    @ApiOperation(value = "获取友情链接信息")
    @PostMapping("/add")
    public R add(@RequestBody CmsLinks cmsLinks) {
        try {
            if (!StringUtils.isEmpty(cmsLinks)) {
                linksService.save(cmsLinks);
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            return R.error();
        }
    }

    @ApiOperation(value = "修改友情链接信息")
    @PostMapping("/edit")
    public R edit(@RequestBody CmsLinks cmsLinks) {
        try {
            if (!StringUtils.isEmpty(cmsLinks.getId())) {
                linksService.updateById(cmsLinks);
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            return R.error();
        }
    }

    @ApiOperation(value = "删除友情链接信息")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable("id") Integer id) {
        try {
            linksService.removeById(id);
            return R.ok();
        } catch (Exception e) {
            return R.error();
        }
    }
}
